package cn.pzhu.pserson.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import cn.pzhu.pserson.service.ICodeService;
import cn.pzhu.pserson.util.ResponseUtil;
import cn.pzhu.pserson.dto.CodeDTO;
import cn.pzhu.pserson.dto.LimitShowDTO;
import cn.pzhu.pserson.model.Code;
import org.springframework.web.servlet.ModelAndView;


@Controller
@RequestMapping("/code")
public class CodeController {
    @Autowired
    private ICodeService codeService;

    @RequestMapping(value = "/page", method = RequestMethod.GET)
    public ModelAndView list( Model model,
        @Validated
        LimitShowDTO limitShowDTO, BindingResult errors) {

        Map<String, Object> result = new HashMap<String, Object>();

        List<Map<String, Object>> codeList = codeService.loadMaps(null, null, null, null, limitShowDTO.getPageNum(), limitShowDTO.getPageSize());
        long count = codeService.count();

        model.addAttribute("list", codeList);

        return new ModelAndView("/code/list", (Map<String, ?>) model);
    }

    @RequestMapping(value = "/admin/update", method = RequestMethod.GET)
    public ModelAndView updatepage( Model model,
                              @Validated
                                      LimitShowDTO limitShowDTO, BindingResult errors) {

        Map<String, Object> result = new HashMap<String, Object>();

        List<Map<String, Object>> codeList = codeService.loadMaps(null, null, null, null, limitShowDTO.getPageNum(), limitShowDTO.getPageSize());
        long count = codeService.count();

        model.addAttribute("list", codeList);

        return new ModelAndView("/code/update", (Map<String, ?>) model);
    }


    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResponseEntity<String> show() {
        Map<String, Object> codeModel = codeService.findMap();

        return ResponseUtil.getResEntityForGetAndJson(codeModel);
    }

    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResponseEntity<String> create(
        @Validated
        CodeDTO codeDTO, BindingResult errors) {
        if(errors.hasErrors())
            return ResponseUtil.getResponseEntityWhenInvalidReqParams();

        Code codeModel = codeDTO.toModel();

        return ResponseUtil.getResEntityForPPP(codeService.save(codeModel));
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ModelAndView update(Model model,
        @Validated
        CodeDTO codeDTO, BindingResult errors) {

        Code codeModel = codeDTO.toModel();

        Boolean state = codeService.update(codeModel);

        List<Map<String, Object>> codeList = codeService.loadMaps(null, null, null, null, 0, 1);

        model.addAttribute("list", codeList);

        return new ModelAndView("/code/list");
    }

    @RequestMapping(value = "", method = RequestMethod.DELETE)
    public ResponseEntity<String> destory() {
        return ResponseUtil.getResEntityForDel(codeService.delete());
    }

}
